package com.ss.android.article.base.feature.detail2.video;

import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.ss.android.article.base.feature.detail.model.ArticleInfo;
import com.ss.android.article.base.feature.detail.model.PhoneAdsData;
import com.ss.android.article.base.feature.detail2.video.a.NewVideoFragmentHelper;
import com.ss.android.article.base.feature.detail2.video.a.l;
import com.ss.android.article.base.feature.feed.c.FeedDownloadAppAd;
import com.ss.android.article.base.feature.model.AppAd;
import com.ss.android.article.base.feature.model.DownloadAppAd;
import com.ss.android.article.base.feature.model.VideoAlbumInfo;
import com.ss.android.article.base.feature.video.IVideoController;
import com.ss.android.article.browser.R;
import com.ss.android.b.a.BaseAdsData;
import com.ss.android.common.lib.MobClickCombiner;
import com.ss.android.common.callback.CallbackCenter;
import com.ss.android.common.ad.MobAdClickCombiner;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.common.util.ToolUtils;

import org.json.JSONException;
import org.json.JSONObject;

final class g implements AbsListView.OnScrollListener {
    private final NewVideoDetailFragment a;

    g(NewVideoDetailFragment arg1) {
        this.a = arg1;

    }

    public final void onScroll(AbsListView arg15, int arg16, int arg17, int arg18) {
        int v5;
        int v4_1;
        int[] v3_4;
        JSONObject v10 = null;
        int v3_1;
        int[] v2_2;
        int v2_1;
        NewVideoFragmentHelper v12 = this.a.mNewVideoFragmentHelper;
        if (v12 != null) {
            NewVideoDetailFragment v4 = this.a;
            if (v12 != null && !v12.n && v12.e != null && v12.e.t != null && v12.e.t.getVisibility() == View.VISIBLE && v12.e.H != null) {
                l v2 = v12.e;
                if (v2.H == null || v2.H.aa == null) {
                    v2_1 = 0;
                } else {
                    com.ss.android.article.base.feature.detail.model.ArticleInfo.a v3 = v2.H.aa;
                    if (v3.mBannerAdsData == null || !v3.mBannerAdsData.isValid()) {
                        if (v3.a != null && (v3.a.isValid())) {
                            v2_1 = 1;
                        } else if (v3.d != null && (v3.d.isValid())) {
                            v2_1 = 1;
                        } else if (v3.b != null && (v3.b.isValid())) {
                            v2_1 = 1;
                        } else if (v3.g != null && (v3.g.isValid())) {
                            v2_1 = 1;
                        } else if (v3.f != null && (v3.f.isValid()) && !ToolUtils.isAppInstalled(v2.g, v3.f.pkg)) {
                            v2_1 = 1;
                        } else {
                            v2_1 = 0;
                        }
                    } else {
                        v2_1 = 1;
                    }
                }

                if (v2_1 != 0) {
                    v2_2 = new int[2];
                    v12.mContentLayout.getLocationOnScreen(v2_2);
                    v3_1 = v2_2[1] + v12.mContentLayout.getHeight();
                    v12.e.t.getLocationOnScreen(v2_2);
                    if (v2_2[1] < 0) {
                    } else if (v2_2[1] > v3_1) {
                    } else {
                        v4.a(v12.e.H.mBannerAdsData);
                        v4.a(v12.e.H.A);
                        v4.a(v12.e.H.B);
                        DownloadAppAd v2_3 = v12.e.H.mDownloadAppAd;
                        if (v2_3 != null && (((AppAd) v2_3).isValid()) && !ToolUtils.isAppInstalled(v4.context, ((AppAd) v2_3).pkg) && (((AppAd) v2_3).K)) {
                            BaseAdsData.trackAdShow(v4.context, "detail_ad", ((BaseAdsData) v2_3), 1);
                        }

                        PhoneAdsData v2_4 = v12.e.H.z;
                        if (v2_4 != null && (v2_4.isValid()) && (v2_4.K)) {
                            BaseAdsData.trackAdShow(v4.context, "detail_call", ((BaseAdsData) v2_4), 1);
                        }

                        v4.a(v12.e.H.y);
                        ArticleInfo v6 = v12.e.H;
                        if (v6 != null && v6.E != null && (v6.E.isValid())) {
                            JSONObject v3_2 = null;
                            try {
                                v10 = new JSONObject();
                                v10.put("log_extra", v6.E.logExtra);
                                v10.put("item_id", v6.groupItemId);
                                if (v6.mMediaInfo == null) {
                                } else {
                                    v10.put("media_id", v6.mMediaInfo.id);
                                }
                            } catch (JSONException v2_5) {
                                v2_5.printStackTrace();
                            }
                            MobAdClickCombiner.onAdEvent(v4.context, "detail_ad", "show", v6.E.id, 0, v10, 1);
                        }

                        v12.n = true;
                    }
                }
            }

            this.a.a(v12);
            this.a.b(v12);
            NewVideoDetailFragment v11 = this.a;
            if (v12 != null && v12.e != null && v12.e.H != null && (!v12.e.H.videoSubjectList.isEmpty() && !v12.r)) {
                v2_2 = new int[2];
                v12.mContentLayout.getLocationOnScreen(v2_2);
                v3_1 = v2_2[1] + v12.mContentLayout.getHeight();
                v12.e.n.getLocationOnScreen(v2_2);
                if (v2_2[1] < 0) {
                } else if (v2_2[1] >= v3_1) {
                } else {

                    v12.r = true;
                    Object v2_6 = v12.e.H.videoSubjectList.get(0);
                    if (v2_6 == null) {
                    } else if (v11.c) {
                    } else {
                        v10 = null;
                        if (((VideoAlbumInfo) v2_6).videoSubject > 0) {
                            v10 = new JSONObject();
                            try {
                                v10.put("video_subject_id", ((VideoAlbumInfo) v2_6).videoSubject);
                            } catch (JSONException v3_3) {
                            }
                        }

                        MobClickCombiner.onEvent(v11.context, "video", "detail_album_show", ((VideoAlbumInfo) v2_6).mediaId, 0, v10);
                        ((VideoAlbumInfo) v2_6).j = ((com.ss.android.article.base.feature.detail2.bb) v11);
                    }
                }
            }
            NewVideoDetailFragment v2_7 = this.a;
            if (v12 != null && !v12.t && v12.e != null && v12.e.H != null && v12.e.H.relatedVideoImageInfo != null) {
                v3_4 = new int[2];
                v12.mContentLayout.getLocationOnScreen(v3_4);
                v4_1 = v3_4[1] + v12.mContentLayout.getHeight();
                v12.e.m.getLocationOnScreen(v3_4);
                if (v3_4[1] >= 0 && v3_4[1] < v4_1) {
                    v12.t = true;
                    MobClickCombiner.onEvent(v2_7.context, "detail", "ad_video_show");
                }
            }

            v2_7 = this.a;
            if (v12 != null && v12.mVideoDetailExtraHolder != null && v12.mVideoDetailExtraHolder.L != null && (v12.mVideoDetailExtraHolder.L.b != null && v12.mVideoDetailExtraHolder.L.h != null)) {
                FeedDownloadAppAd v6_1 = v12.mVideoDetailExtraHolder.L.h;
                v3_4 = new int[2];
                v12.mContentLayout.getLocationOnScreen(v3_4);
                v4_1 = v3_4[1];
                v5 = v12.mContentLayout.getHeight() + v4_1;
                v12.mVideoDetailExtraHolder.L.b.getLocationOnScreen(v3_4);
                if (v3_4[1] >= v4_1 && v3_4[1] < v5) {
                    if (!v12.u) {
                        v12.u = true;
                        try {
                            v10 = new JSONObject();
                            if (!android.support.design.a.isTextEmpty(v6_1.logExtra)) {
                                v10.put("log_extra", v6_1.logExtra);
                            }

                            MobAdClickCombiner.onAdEvent(v2_7.context, "embeded_ad", "detail_show", v6_1.id, 0, v10, 1);
                        } catch (Exception v2_8) {
                        }
                    } else {
                    }
                } else {
                    v12.u = false;
                }
            }

            if (v12.e != null) {
                l v6_2 = v12.e;
                int[] v7 = this.a.A;
                if (v6_2.K != null && v7 != null) {
                    LinearLayout v2_9 = null;
                    LinearLayout v4_2;
                    switch (v6_2.L) {
                        case 8: {
                            v4_2 = v6_2.q;
                            break;
                        }
                        case 9: {
                            v4_2 = v2_9;
                            break;
                        }
                        case 10: {
                            v6_2.g.getResources().getDimensionPixelOffset(R.dimen.h8);
                            v4_2 = v2_9;
                            break;
                        }
                        default:
                            v4_2 = v2_9;
                            break;
                    }
                    if (v4_2 != null) {
                        v5 = ((ViewGroup) v4_2).getChildCount();
                        if (v5 > 0) {
                            v3_1 = -1;
                            v2_1 = -1;
                            if (((ViewGroup) v4_2).getVisibility() == View.VISIBLE && v7[1] > v7[0]) {
                                View v8 = ((ViewGroup) v4_2).getChildAt(0);
                                View v4_3 = ((ViewGroup) v4_2).getChildAt(v5 - 1);
                                int v9 = v8.getHeight();
                                if (v9 > 0) {
                                    v8.getLocationInWindow(v6_2.J);
                                    int v8_1 = v6_2.J[1];
                                    v4_3.getLocationInWindow(v6_2.J);
                                    v4_1 = v6_2.J[1] + v9;
                                    if (v8_1 <= v7[1] && v4_1 >= v7[0]) {
                                        v2_1 = v8_1 < v7[0] ? (v7[0] - v8_1) / v9 : 0;
                                        if (v4_1 < v7[1]) {
                                            v3_1 = 1;
                                            v4_1 = v5;
                                        } else {
                                            v4_1 = (v7[1] - v8_1) / v9;
                                            v3_1 = (v7[1] - v8_1) % v9 == 0 ? 1 : 0;
                                        }

                                        int v13 = Math.min(v4_1 - v3_1, v5 - 1);
                                        v3_1 = v2_1;
                                        v2_1 = v13;
                                    }
                                    v6_2.K.b(v3_1, v2_1);
                                }
                            } else {
                                v6_2.K.b(v3_1, v2_1);
                            }
                        }
                    }
                }
            }

            v3_1 = arg16 + arg17;
            v4 = this.a;
            boolean v2_10 = v3_1 > ((ListView) arg15).getHeaderViewsCount() ? true : false;
            v4.r = v2_10;
            this.a.o();
            if (!this.a.s && (this.a.r)) {
                v12.mNewDetailAdapter.notifyDataSetChanged();
                this.a.s = true;
            }

            if (v3_1 >= arg18 && arg18 > ((ListView) arg15).getHeaderViewsCount() + ((ListView) arg15).getFooterViewsCount() && (NetworkUtils.isNetworkAvailable(this.a.context))) {
                v2_7 = this.a;
                v2_1 = v12 == null || v12.mCommentTabCache == null || (v2_7.q()) || (v12.mCommentTabCache.load[0]) || !v12.mCommentTabCache.mArticleTabComments[0].hasMore ? -1 : 0;
                if (v2_1 < 0) {
                    return;
                }

                this.a.v();
            }
        }
    }

    public final void onScrollStateChanged(AbsListView arg6, int arg7) {
        NewVideoFragmentHelper v1 = this.a.mNewVideoFragmentHelper;
        if (v1 != null) {
            CallbackCenter.notifyCallback(IVideoController.STOP_END_COVER, new Object[0]);
            if (v1.mListView != null) {
                this.a.k = v1.mListView.getFirstVisiblePosition();
                if (v1.mListView.getChildAt(0) != null) {
                    this.a.l = v1.mListView.getChildAt(0).getTop();
                }
            }

            if (!v1.mNewDetailAdapter.h) {
                v1.mNewDetailAdapter.h = true;
            }

            int v0 = this.a.l > 0 ? this.a.l : -this.a.l;
            this.a.a(v0);
            if (arg7 == 0 && v1.mListView != null) {
                View v0_1 = v1.mListView.getChildAt(0);
                int v1_1 = v1.mListView.getFirstVisiblePosition();
                if (v0_1 != null && v0_1.getTop() == 0 && v1_1 == 0) {
                    CallbackCenter.notifyCallback(IVideoController.RESHOW_END_COVER, new Object[0]);
                }
            }

            if (arg7 == 0) {
                return;
            }

            this.a.B = true;
            if (this.a.mMonitorFPS == null) {
                return;
            }

            this.a.mMonitorFPS.b();
        }
    }
}

